<?php

/**
 * This is the model class for table "{{slide}}".
 *
 * The followings are the available columns in table '{{slide}}':
 * @property string $id
 * @property string $name
 * @property string $slide_images
 * @property string $slide_link
 * @property integer $slide_order
 * @property integer $slide_show
 * @property string $slide_short
 */
class Slide extends CActiveRecord {

    /**
     * Returns the static model of the specified AR class.
     * @param string $className active record class name.
     * @return Slide the static model class
     */
    public $slide_link;
    public $slide_short;
    public $slide_show;
    public $slide_order;

    public static function model($className = __CLASS__) {
        return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName() {
        return '{{slide}}';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules() {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('name, slide_images, slide_link, slide_order, slide_show', 'required'),
            array('slide_order, slide_show', 'numerical', 'integerOnly' => true),
            array('slide_images', 'length', 'max' => 50),
            array('slide_short', 'safe'),
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            array('id, name, slide_images, slide_link, slide_order, slide_show, slide_short', 'safe', 'on' => 'search'),
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations() {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels() {
        return array(
            'id' => 'ID',
            'name' => 'Name',
            'slide_images' => 'Slide Images',
            'slide_link' => 'Slide Link',
            'slide_order' => 'Slide Order',
            'slide_show' => 'Slide Show',
            'slide_short' => 'Slide Short',
        );
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
     */
    public function search() {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria = new CDbCriteria;

        $criteria->compare('id', $this->id, true);
        $criteria->compare('name', $this->name, true);
        $criteria->compare('slide_images', $this->slide_images, true);
        $criteria->compare('slide_link', $this->slide_link, true);
        $criteria->compare('slide_order', $this->slide_order);
        $criteria->compare('slide_show', $this->slide_show);
        $criteria->compare('slide_short', $this->slide_short, true);

        return new CActiveDataProvider($this, array(
            'criteria' => $criteria,
        ));
    }

    public static function Resources() {
        return array(
            'thumbnail' => array('type' => 'thumbnail',
                'name' => 'Thumbnail',
                'maxSize' => UPLOAD_MAX_SIZE,
                'minSize' => UPLOAD_MIN_SIZE,
                'max' => 1,
                'allow' => array('jpeg',
                    'jpg',
                    'gif',
                    'png')));
    }

    public static function getResourceObjectFromDb($model, $key, $slide_id) {
        $upload_files = array();
        $resources = Slide::model()->findAll(array(
            'condition' => 'id = :obj',
            'params' => array(':obj' => $slide_id)));
        if ($resources & count($resources) > 0) {
            foreach ($resources as $res) {
                if ($find_resource = Slide::model()->findByPk($res->id)) {
                    $file = array();
                    $file['link'] = RESOURCE_URL . "/" . $find_resource->slide_images; //$find_resource->getFullPath();                
                    $file['resid'] = $slide_id;
                    $file['type'] = "image";
                    $upload_files[] = $file;
                }
            }
        }
        return $upload_files;
    }

}
